package com.ruoyi.web.controller.pay;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pay.entity.Commodity;
import com.ruoyi.pay.entity.OrderInfo;
import com.ruoyi.pay.entity.PaymentInfo;
import com.ruoyi.pay.entity.RefundInfo;
import com.ruoyi.pay.service.CommodityService;
import com.ruoyi.pay.service.OrderInfoService;
import com.ruoyi.pay.service.PaymentInfoService;
import com.ruoyi.pay.service.RefundInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

@RestController
@Api(tags = "支付业务数据账单")
@RequestMapping("/pay")
public class PayBusinessController extends BaseController {


    @Resource
    private CommodityService commodityService;

    @Resource
    private OrderInfoService orderInfoService;

    @Resource
    private PaymentInfoService paymentInfoService;

    @Resource
    private RefundInfoService refundInfoService;

    @ApiOperation("当前正在上架的商品列表")
    @GetMapping("/commodity/listing")
    public R<List<Commodity>> listing() {
        List<Commodity> list = commodityService.getListing();
        return R.ok(list);
    }

    @ApiOperation("商品列表、搜索")
    @RepeatSubmit(interval = 1000, message = "请求过于频繁")
    @GetMapping("/commodity/list")
    public TableDataInfo list(String searchTitle) {

        if(StringUtils.isNull(searchTitle)) searchTitle = "";

        startPage();
        List<Commodity> list = commodityService.getList(searchTitle);

        return getDataTable(list);
    }

    @ApiOperation("改变商品上下架状态")
    @PostMapping("/commodity/updateStatus/{id}/{status}")
    public R updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        commodityService.updateStatus(id, status);
        return R.ok();
    }

    @ApiOperation("商品详情")
    @GetMapping("/commodity/detail/{id}")
    public R<Commodity> detail(@PathVariable Long id) {
        Commodity commodity = commodityService.getById(id);
        return R.ok(commodity);
    }

    @ApiOperation("新增商品")
    @PostMapping("/commodity/add")
    public R add(@RequestBody Commodity commodity) {
        commodityService.save(commodity);
        return R.ok();
    }

    @ApiOperation("删除商品")
    @PostMapping("/commodity/delete/{id}")
    public R delete(@PathVariable Long id) {
        commodityService.removeById(id);
        return R.ok();
    }


    @ApiOperation("订单列表")
    @GetMapping("/order/list")
    public TableDataInfo orderList() {

        // 时间逆序
        LambdaQueryWrapper<OrderInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(OrderInfo::getCreateTime);

        startPage();
        List<OrderInfo> list = orderInfoService.list(wrapper);

        return getDataTable(list);
    }

    @ApiOperation("订单详情")
    @GetMapping("/order/detail/{id}")
    public R<OrderInfo> orderDetail(@PathVariable String id) {
        OrderInfo orderInfo = orderInfoService.getById(id);
        return R.ok(orderInfo);
    }


    @ApiOperation("支付日志")
    @GetMapping("/payment/list")
    public TableDataInfo PaymentList() {

        // 时间逆序
        LambdaQueryWrapper<PaymentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(PaymentInfo::getCreateTime);

        startPage();
        List<PaymentInfo> list = paymentInfoService.list(wrapper);

        return getDataTable(list);
    }

    @ApiOperation("退款日志列表")
    @GetMapping("/refund/list")

    public TableDataInfo RefundList() {

        // 时间逆序
        LambdaQueryWrapper<RefundInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(RefundInfo::getCreateTime);

        startPage();
        List<RefundInfo> list = refundInfoService.list(wrapper);

        return getDataTable(list);
    }
}
